package com.shangxia.leetcode.hoot100.easy;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

/**
 * @ProjectName: leetcode
 * @ClassName: MaxDepth104
 * @Time: 2020/12/1
 * @Author: zhouy
 * @Description:
 * @Experience:
 */

@Slf4j
public class MaxDepth104 {
    public static void main(String[] args) {
        System.out.println(maxDepth(new TreeNode(1,null
                ,new TreeNode(2))));

    }


    public static int maxDepth(TreeNode root) {
        int deep = 0;
        if (null == root) {
            return 0;
        }
        deep++;
        if (null == root.left) {
            deep += maxDepth(root.right);
            return deep;
        }else if (null == root.right) {
            deep += maxDepth(root.left);
        }else {
            deep+=Math.max(maxDepth(root.right),maxDepth(root.left));
        }
        return deep;
    }
    public static int maxDepth2(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            int leftHeight = maxDepth(root.left);
            int rightHeight = maxDepth(root.right);
            return Math.max(leftHeight, rightHeight) + 1;
        }
    }

    public static int maxDepth3(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        int ans = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
                size--;
            }
            ans++;
        }
        return ans;
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(Integer val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
